package jia.game;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

/**
 * @author Alec Lanter
 */
public class CoreGameFrame extends JPanel{
    private static final Logger log = Logger.getLogger(CoreGameFrame.class);
    private BufferedImage skinImage;
    
    /**
     */
    public CoreGameFrame(){
        setOpaque(false);
    }
    
    /**
     * @param skinImageFile
     */
    public void setSkinImage(String skinImageFile){
        URL skinImageFileUrl = getClass().getClassLoader().getResource(skinImageFile);
        try {
            skinImage = ImageIO.read(skinImageFileUrl);
            if(skinImage == null) {
                if (GenericFlags.warningMessages) {
                    log.warn("Couldn't load skin resource -- non-exception");
                }
            }
            if(GenericFlags.debugMessages) {
                log.debug("Image is " + skinImage.getWidth() + "x" + skinImage.getHeight());
            }
        } catch(IOException e) {
            if (GenericFlags.errorMessages){
                log.error("Couldn't load skin resource '" + skinImageFile + "' -- using blank");
            }
        }
    }
    
    /**
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    protected void paintComponent(Graphics g){
        if(skinImage != null) {
            if(GenericFlags.debugMessages) {
                log.debug("Painting component");
            }
            g.drawImage(skinImage, 0, 0, null);
        }
        super.paintComponent(g);
    }
}
